<template>
  <div class="manager-container">
    <!--  头部  -->
    <div class="manager-header">
      <div class="manager-header-left">
        <img src="@/assets/imgs/logo.png" />
        <div class="title">报修系统</div>
      </div>

      <div class="manager-header-center">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="manager-header-right">
        <el-dropdown placement="bottom">
          <div class="avatar">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <div>{{ user.name ||  '管理员' }}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goToPerson">个人信息</el-dropdown-item>
            <!-- <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item> -->
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <div v-if="isMobile" @click="toggleSidebar" class="hamburger-menu">
          <i class="el-icon-menu"></i>
        </div>
      </div>
    </div>

    <!--  主体  -->
    <div class="manager-main">
      <!--  侧边栏  -->
      <div :class="['manager-main-left', { 'hidden': isMobile &&!sidebarVisible }]">
        <el-menu :default-openeds="['info', 'user']" router style="border: none" :default-active="$route.path">
          <el-menu-item index="/homec" v-if="user.role === 'ADMIN'">
            <i class="el-icon-s-home"></i>
            <span slot="title" v-if="user.role === 'ADMIN'">报修单统计</span>
            <span slot="title" v-if="user.role === 'USER'">首页</span>
            <span slot="title" v-if="user.role === 'WORKER'">首页</span>
          </el-menu-item>
          <el-submenu index="info"  v-if="user.role === 'ADMIN'">
            <template slot="title">
              <i class="el-icon-menu"></i><span>基础数据管理</span>
            </template>
            <el-menu-item index="/category" v-if="user.role === 'ADMIN'">维修类型</el-menu-item>
            <el-menu-item index="/state" v-if="user.role === 'ADMIN'">维修状态</el-menu-item>
            <el-menu-item index="/repair" v-if="user.role === 'ADMIN'">报修单管理</el-menu-item>
          </el-submenu>

          <el-submenu index="user" v-if="user.role === 'USER'">
            <template slot="title">
              <i class="el-icon-menu"></i><span>操作页面</span>
            </template>
            <!-- 根据设备类型显示不同菜单项 -->
            <el-menu-item index="/userRepairAdd" v-if="isMobile && user.role === 'USER'">报修新增</el-menu-item>
            <el-menu-item index="/userRepairCheck" v-if="isMobile && user.role === 'USER'">历史报修</el-menu-item>
            <el-menu-item index="/userRepair" v-if="!isMobile && user.role === 'USER'">用户报修</el-menu-item>
          </el-submenu>

          <el-submenu index="user" v-if="user.role === 'WORKER'">
            <template slot="title">
              <i class="el-icon-menu"></i><span>操作页面</span>
            </template>
            <el-menu-item index="/workerRepairCheck" v-if="isMobile">维修管理</el-menu-item>
            <el-menu-item index="/workerRepair" v-if="!isMobile">维修管理</el-menu-item>
          </el-submenu>
        </el-menu>
      </div>

      <!--  数据表格  -->
      <div class="manager-main-right">
        <router-view @update:user="updateUser" />
      </div>
    </div>

    <!-- 遮罩层 -->
    <div
      v-if="isMobile && sidebarVisible"
      @click="toggleSidebar"
      class="sidebar-mask"
    ></div>

    <!-- 悬浮按钮 -->
    <div
      v-if="isMobile"
      @click="toggleSidebar"
      class="floating-button"
    >
      <i :class="sidebarVisible? 'el-icon-close' : 'el-icon-menu'"></i>
    </div>
  </div>
</template>

<script>
export default {
  name: "Manager",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      isMobile: window.innerWidth < 768,
      sidebarVisible: false
    }
  },
  created() {
    if (!this.user.id) {
      this.$router.push('/login')
    }
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    goToPerson() {
      if (this.user.role === 'ADMIN') {
        this.$router.push('/adminPerson')
      } else if (this.user.role === 'USER') {
        this.$router.push('/userPerson')
      } else if (this.user.role === 'WORKER') {
        this.$router.push('/workerPerson')
      }
    },
    logout() {
      localStorage.removeItem('xm-user')
      this.$router.push('/login')
    },
    handleResize() {
      this.isMobile = window.innerWidth < 768;
      if (!this.isMobile) {
        this.sidebarVisible = true;
      }
    },
    toggleSidebar() {
      this.sidebarVisible =!this.sidebarVisible;
    }
  }
}
</script>

<style scoped>
@import "@/assets/css/manager.css";

.manager-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
}

.manager-header-left {
  display: flex;
  align-items: center;
}

.manager-header-left img {
  height: 30px;
  margin-right: 10px;
}

.manager-header-center {
  flex-grow: 1;
  padding: 0 10px;
}

.manager-header-right {
  display: flex;
  align-items: center;
}

.avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.avatar img {
  height: 30px;
  width: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.hamburger-menu {
  display: none;
  cursor: pointer;
  margin-left: 10px;
}

@media (max-width: 767px) {
  .manager-header-center {
    display: none;
  }

  .hamburger-menu {
    display: block;
  }

  .manager-main-left {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 200px;
    background-color: white;
    z-index: 1000;
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.2);
    transition: transform 0.3s ease-in-out;
    transform: translateX(-100%);
  }

  .manager-main-left:not(.hidden) {
    transform: translateX(0);
  }

  .manager-main-right {
    padding: 10px;
  }

  .sidebar-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
  }

  /* 悬浮按钮样式 */
  .floating-button {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    background-color: #007bff;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 24px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    z-index: 1001;
  }
}
</style>